package uk.co.hephistocles.blobGame;

import java.util.LinkedList;
import java.util.Vector;

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector3;

public class BezierPath implements Path {
	private float totLength;
	private Vector<Bezier> curves;
	public static final float linePrecision = 1f;

	@Override
	public void init(FileHandle fileHandle) {
		String[] bezFile = fileHandle.readString().split("\r");
		int count = bezFile.length;
		curves = new Vector<Bezier>();
		LinkedList<Vector3> pts = new LinkedList<Vector3>();
		//loop through the file to get lines of the Bezier curve
		for (int i=1; i<count; i++) {
			String[] o = bezFile[i-1].split(" ");
			String[] c = bezFile[i].split(" ");
			if (bezFile[i].length() > 3) {
				pts.clear();
				//the first control point of a Bezier is the last of the previous, in a spline
				pts.add(new Vector3(Float.parseFloat(o[o.length-3]),Float.parseFloat(o[o.length-2]),0));
				for (int j=0; j<c.length/2; j++) {
					pts.addLast(new Vector3(Float.parseFloat(c[2*j]),Float.parseFloat(c[2*j+1]),0));
				}
				curves.add(new Bezier((LinkedList<Vector3>) pts.clone()));
			} else {
				i++;
			}
		}
	}
	
	public void init(Vector<Bezier> curves) {
		this.curves = curves;
	}
	
	@Override
	public int getSegmentsNum() {
		return curves.size();
	}
	
	public Vector<Line> toLines() {
		Vector<Line> linez = new Vector<Line>();
		
		for (int i=0;i<curves.size();i++) {
			linez.addAll(Bezier.toLines(curves.get(i), linePrecision));
		}
		return linez;
	}

	@Override
	public Vector3 getPos(int seg, float t) {
		Vector3 ans = curves.get(seg).getPos(t);
		return ans;
	}

	@Override
	public Vector3 getPosFromDist(int seg, float s) {
		// TODO Auto-generated method stub
		return null;
	}
}
